<?php defined('SYSPATH') or die('No direct access allowed.');

class Controller_Admin_Section extends Controller_Admin_Template
{
   
    
    public function action_index()
    {
        $sections = View::factory('admin/section/section')
                        ->set('sections', $this->get_sections());
        $this->template->content = $sections->render();        
    }
    
    public function get_sections($id_parent = 0)
    {
        $sections = array();
        $query = DB::query(Database::SELECT, "SELECT c1.id as `id`, c1.name as `name`, c1.href as `href`, c1.hide as `hide`, c1.id_parent as `id_parent`, (COUNT( c2.name )>0) as `subsections` 
                                                FROM  `sections` AS  `c1` 
                                                LEFT JOIN  `sections` AS  `c2` ON c2.id_parent = c1.id
                                                WHERE c1.id_parent =$id_parent                                                
                                                GROUP BY c1.name
                                                ORDER BY c1.pos");
        $result = $query->execute();        
        foreach ($result as $section) {
            if($section['subsections'])
                $section['subsections'] = $this->get_sections($section['id']);
            else
                unset($section['subsections']);
            $sections[] = $section;
        }
        return $sections;
    }
    
    public function action_add()
    {
        if($_POST){            
            $section = ORM::factory('section');
           
            $section->values($_POST);
            try
            {
                $section->save();
                
                $query = DB::query(Database::SELECT, "SELECT MAX(pos) as `pos`
                                                      FROM `sections`
                                                      WHERE `id_parent`={$section->id_parent}");
                $pos = $query->execute()->get('pos') + 1;
                
                $section->pos = $pos;
                $section->save();
                
                Request::$initial->redirect('admin/section');
            }
            catch(ORM_Validation_Exception $e)
            {
                $errors = $e->errors('admin');
            }
        }
        
        $this->template->styles = array('css/admin/box.css' => 'screen');        
        $this->template->content = View::factory('admin/section/form')
                                       ->bind('errors', $errors)
                                       ->set('title', 'Добавление нового раздела')
                                       ->set('button', 'Добавить')
                                       ->set('data', $_REQUEST); 
    }
    
    public function action_hide()
    {
        $id = Arr::get($_REQUEST, 'id', -1);
        if($id != -1){
            $hide = ORM::factory('section', $id)->__get('hide');
            if($hide == 'hide')
                $this->show($id);
            else
                $this->hide($id);
        }
        
        Request::$initial->redirect('admin/section');
    }
    
    public function hide($id)
    {
        ORM::factory('section', $id)->set('hide','hide')->save();
           
        $sections = ORM::factory('section')->where('id_parent', '=', $id)->find_all();
        foreach ($sections as $section){
            $this->hide($section->id);
        }    
    }
    
    public function show($id)
    {
        if($id != 0){
            $section = ORM::factory('section', $id);
            $section->hide = 'show';
            $section->save();
            
            $this->show($section->id_parent);
        }
    }
    
    public function action_edit()
    {
        $id = Arr::get($_REQUEST, 'id', 0);
        
        if($id != 0){
            $section = ORM::factory('section', $id);
            if($_POST){
                $section->values($_POST);
                try
                {
                    $section->save();
                    Request::$initial->redirect('admin/section');
                }
                catch(ORM_Validation_Exception $e)
                {
                    $errors = $e->errors('admin');
                }
            }
            $data = $section->as_array();
        }
        
        $this->template->styles = array('css/admin/box.css' => 'screen');        
        $this->template->content = View::factory('admin/section/form')
                                       ->bind('errors', $errors)
                                       ->set('title', 'Редактирование раздела')
                                       ->set('button', 'Сохранить изменения')
                                       ->bind('data', $data);    
    }
    
    public function action_delete()
    {
        $this->delete(Arr::get($_REQUEST, 'id', 0));
        
        Request::$initial->redirect('admin/section');
    }
    
    public function delete($id)
    {
        if($id != 0)
        {
            $sections = ORM::factory('section')->where('id_parent', '=', $id);
            foreach ($sections as $section) {
                delete($section->id);
            }
            ORM::factory('section', $id)->delete();            
        }
    }
    
    public function action_up()
    {
        $id = Arr::get($_REQUEST, 'id', 0);
        if($id != 0) {
            $section = ORM::factory('section', $id);
            
            $query = DB::query(Database::SELECT, "SELECT `id`, `pos`
                                                  FROM `sections`
                                                  WHERE (`pos` < {$section->pos} AND `id_parent`={$section->id_parent})
                                                  ORDER BY `pos` DESC
                                                  LIMIT 1");
            
            $result = $query->execute();
            if($result->count() > 0){
                $up_id = $result->get('id');
                $up_pos = $result->get('pos');
                
                $up_section = ORM::factory('section', $up_id);
                $up_section->pos = $section->pos;
                $up_section->save();
                
                $section->pos = $up_pos;
                $section->save();
            }
        }
        
        Request::$initial->redirect('admin/section');
    }
    
    public function action_down()
    {
        $id = Arr::get($_REQUEST, 'id', 0);
        if($id != 0) {
            $section = ORM::factory('section', $id);
            
            $query = DB::query(Database::SELECT, "SELECT `id`, `pos`
                                                  FROM `sections`
                                                  WHERE (`pos` > {$section->pos} AND `id_parent`={$section->id_parent})
                                                  ORDER BY `pos` ASC
                                                  LIMIT 1");
            
            $result = $query->execute();
            if($result->count() > 0){
                $down_id = $result->get('id');
                $down_pos = $result->get('pos');
                
                $down_section = ORM::factory('section', $down_id);
                $down_section->pos = $section->pos;
                $down_section->save();
                
                $section->pos = $down_pos;
                $section->save();
            }
        }
        
        Request::$initial->redirect('admin/section');
    }
}